﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using ViCode_LeVi.Controls.Converters;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabImages_Item.xaml
    /// </summary>
    public partial class VMTabImages_Item : System.Windows.Controls.UserControl
    {
        public VMTabImages_Item()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabImages_Item_Loaded);
        }

        void VMTabImages_Item_Loaded(object sender, RoutedEventArgs e)
        {            
            LoadImage();
        }

        public void LoadImage()
        {
            try
            {
                if (IndexImage >= 0 && IndexImage < ProjectData.Intance.ImageInfos.Length)
                {
                    var pic = ProjectData.Intance.ImageInfos[IndexImage];
                    if (pic != null)
                    {
                        try
                        {
                            Image_Main.Source = BinaryToImageConverter.SqlImageToBitmapImage(pic.Info);
                            this.DataContext = ProjectData.Intance.ImageInfos[IndexImage];
                        }
                        catch { }
                    }
                }
            }
            catch { }
        }
        [Browsable(true)]
        public int IndexImage { get; set; }


        private void Browse_Click(object sender, RoutedEventArgs e)
        {
            // open file dialog
            OpenFileDialog open = new OpenFileDialog();
            // image filters
            open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // display image in picture box
                    var bytes = File.ReadAllBytes(open.FileName);
                    Image_Main.Source = BinaryToImageConverter.SqlImageToBitmapImage(bytes);
                    if (IndexImage >= 0 && IndexImage < ProjectData.Intance.ImageInfos.Length)
                    {
                        var pic = ProjectData.Intance.ImageInfos[IndexImage];
                        if (pic != null)
                        {
                            ProjectData.Intance.ImageInfos[IndexImage].Info = bytes;                            
                        }
                    }
                }
                catch { }
            }
        }

        private void Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Image_Main.Source = null;                
                ProjectData.Intance.ImageInfos[IndexImage].Info = null;
                ProjectData.Intance.ImageInfos[IndexImage].Description = string.Empty;
            }
            catch { }
        }
    }
}
